package com.lidroid.xutils.http.callback;

import com.lidroid.xutils.util.IOUtils;
import com.lidroid.xutils.util.OtherUtils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;

/* loaded from: classes.dex */
public class StringDownloadHandler {
    public String handleEntity(HttpEntity httpEntity, RequestCallBackHandler requestCallBackHandler, String str) throws IOException {
        InputStream inputStream = null;
        if (httpEntity == null) {
            return null;
        }
        long j = 0;
        long contentLength = httpEntity.getContentLength();
        if (requestCallBackHandler == null || requestCallBackHandler.updateProgress(contentLength, 0L, true)) {
            StringBuilder sb = new StringBuilder();
            try {
                inputStream = httpEntity.getContent();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, str));
                while (true) {
                    String readLine = bufferedReader.readLine();
                    if (readLine != null) {
                        sb.append(readLine);
                        sb.append('\n');
                        j += OtherUtils.sizeOfString(readLine, str);
                        if (requestCallBackHandler != null && !requestCallBackHandler.updateProgress(contentLength, j, false)) {
                            break;
                        }
                    } else {
                        break;
                    }
                }
                long j2 = j;
                if (requestCallBackHandler != null) {
                    requestCallBackHandler.updateProgress(contentLength, j2, true);
                }
                IOUtils.closeQuietly(inputStream);
                return sb.toString().trim();
            } catch (Throwable th) {
                IOUtils.closeQuietly(inputStream);
                throw th;
            }
        }
        return null;
    }
}
